package com.leyantech.ecp.agenx.common.service;

import com.leyantech.ecp.agenx.common.utils.PbPrintHelper;
import com.leyantech.storehome.AssistantSeatModeInfo;
import com.leyantech.storehome.AssistantSeats;
import com.leyantech.storehome.BlackList;
import com.leyantech.storehome.DeviceNoInfo;
import com.leyantech.storehome.GetAssistantSeatsListRequest;
import com.leyantech.storehome.GetAssistantSeatsListResponse;
import com.leyantech.storehome.GetAssistantSeatsModeInfoRequest;
import com.leyantech.storehome.GetAssistantSeatsModeInfoResponse;
import com.leyantech.storehome.GetDeviceNoInfoRequest;
import com.leyantech.storehome.GetDeviceNoInfoResponse;
import com.leyantech.storehome.GetProductVersionInfoRequest;
import com.leyantech.storehome.GetProductVersionInfoResponse;
import com.leyantech.storehome.GetStoreBlacklistRequest;
import com.leyantech.storehome.GetStoreBlacklistRequest.Builder;
import com.leyantech.storehome.GetStoreBlacklistResponse;
import com.leyantech.storehome.ProductVersionInfo;
import com.leyantech.storehome.StoreHomeServerGrpc.StoreHomeServerBlockingStub;

import com.google.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-16.
 */
@Slf4j
public class StoreHomeService {

  private final StoreHomeServerBlockingStub stub;

  @Inject
  public StoreHomeService(StoreHomeServerBlockingStub stub) {
    this.stub = stub;
  }

  /**
   * 获取 queryProductVersionInfo.
   *
   * @param storeId 门店id.
   * @return 可能查不到的门店版本信息.
   */
  public Optional<ProductVersionInfo> getProductVersionInfo(String storeId) {
    try {
      log.info("getProductVersionInfo sid {}", storeId);
      final GetProductVersionInfoRequest request = GetProductVersionInfoRequest.newBuilder()
          .setStoreId(storeId)
          .build();
      GetProductVersionInfoResponse response = stub.getProductVersionInfo(request);
      log.info("getProductVersionInfo sid {}, res {}", storeId,
          PbPrintHelper.lazyToJson(response));
      return Optional.ofNullable(response).map(GetProductVersionInfoResponse::getData);
    } catch (Exception e) {
      log.error("getProductVersionInfo error, sid {}", storeId, e);
      return Optional.empty();
    }
  }

  public List<AssistantSeats> listStoreAssistant(String storeId) {
    try {
      log.info("getAssistantSeatsList sid {}", storeId);

      final GetAssistantSeatsListRequest request = GetAssistantSeatsListRequest.newBuilder()
          .setStoreId(storeId)
          .build();

      final GetAssistantSeatsListResponse response = stub.getAssistantSeatsList(request);

      log.info("getAssistantSeatsList response {}", PbPrintHelper.lazyToJson(response));
      return response.getDataList();
    } catch (Exception e) {
      log.info("getAssistantSeatsList failed {}", storeId, e);
      return Collections.emptyList();
    }

  }

  public Pair<List<BlackList>, GetStoreBlacklistResponse> getStoreBlackList(String sid) {
    List<BlackList> res = new ArrayList<>();
    GetStoreBlacklistResponse tempResp = null;
    try {
      int pageNo = 1;

      Builder builder = GetStoreBlacklistRequest.newBuilder()
          .setStoreId(sid)
          .setPageSize(20);

      List<BlackList> blackListList;
      do {
        GetStoreBlacklistRequest request = builder.setPageNo(pageNo).build();
        GetStoreBlacklistResponse response = stub.getStoreBlacklist(request);
        tempResp = response;
        log.info("getStoreBlackList: req:{}, res:{}", PbPrintHelper.lazyToJson(request),
            PbPrintHelper.lazyToJson(response));
        pageNo++;
        blackListList = response.getBlackListList();
        res.addAll(blackListList);
      } while (CollectionUtils.isNotEmpty(blackListList) && pageNo < 500);

    } catch (Exception e) {
      log.error("getStoreBlackList failed.", e);
    }
    return Pair.of(res, tempResp);

  }

  /**
   * 查询设备号备注.
   */
  public Optional<List<DeviceNoInfo>> queryDeviceNoConfig(String deviceNo) {
    try {
      GetDeviceNoInfoRequest request = GetDeviceNoInfoRequest.newBuilder()
          .addDeviceNo(deviceNo)
          .build();
      Object reqStr = PbPrintHelper.lazyToJson(request);
      log.info("queryDeviceNoConfig req: {}", reqStr);
      GetDeviceNoInfoResponse response = stub.getDeviceNoInfo(request);
      log.info("queryDeviceNoConfig req: {} ,res: {}", reqStr,
          PbPrintHelper.lazyToJson(response));
      List<DeviceNoInfo> data = response.getDataList();
      if (CollectionUtils.isEmpty(data)) {
        return Optional.empty();
      }
      return Optional.of(data);
    } catch (Exception e) {
      log.error("queryDeviceNoConfig error deviceNo: {}", deviceNo, e);
      return Optional.empty();
    }
  }

  /**
   * 查询自定义客服回复模式.
   */
  public Optional<List<AssistantSeatModeInfo>> getStoreCustomReplyMode(String sid,
      String assistantId) {
    try {
      GetAssistantSeatsModeInfoRequest request = GetAssistantSeatsModeInfoRequest.newBuilder()
          .setStoreId(sid)
          .addAssistantId(assistantId)
          .build();
      Object reqStr = PbPrintHelper.lazyToJson(request);
      log.info("getStoreCustomReplyMode req:{}", reqStr);
      GetAssistantSeatsModeInfoResponse response = stub.getAssistantSeatsModeInfo(
          request);
      log.info("getStoreCustomReplyMode req:{} ,res:{}", reqStr,
          PbPrintHelper.lazyToJson(response));
      List<AssistantSeatModeInfo> storeInfoList = response.getDataList();
      if (CollectionUtils.isEmpty(storeInfoList)) {
        log.warn("当前门店获取自定义回复模式为空.{}", sid);
        return Optional.empty();
      }
      return Optional.of(storeInfoList);
    } catch (Exception e) {
      log.error("当前门店获取自定义回复模式失败:{}", sid, e);
      return Optional.empty();
    }
  }

}
